package com.guigu.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.guigu.health.pojo.*;
import com.guigu.health.service.MenuService;
import com.guigu.health.service.RoleService;
import com.guigu.health.service.UserRolePermissionService;
import com.mysql.jdbc.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:RoleController
 * @Description:角色业务控制类
 * @Author:longtao
 * @Date:2021/4/19
 * @Version:1.0
 */
@RequestMapping(value = "/role")
@Controller
public class RoleController extends BaseController {
    /**
     * 调用roleService服务
     */
    @Reference
    private RoleService roleService;
    /**
     * 调用MenuService服务
     */
    @Reference
    private MenuService menuService;
    /**
     * 调用userRolePermissionService服务
     */
    @Reference
    private UserRolePermissionService userRolePermissionService;

    /**
     * 模糊查询并分页
     *
     * @param name
     * @param pageNum
     * @return
     */
    @RequiresPermissions(value = {"ROLE_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/showRoles", produces = {"application/json;charset=utf-8"}, method = RequestMethod.GET)
    public Object showRoles(String name, String pageNum) {
        System.out.println("name = " + name);
        if (StringUtils.isNullOrEmpty(pageNum)) {
            pageNum = "1";
        }
        PageInfo<RoleUsers> pageInfo = roleService.queryRoleByName(name, Integer.valueOf(pageNum), 2);
        return pageInfo;
    }

    /**
     * 新增用户
     *
     * @param map
     * @return
     */
    @RequiresPermissions(value = {"ROLE_ADD"})
    @ResponseBody
    @RequestMapping(value = "/addRoles", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public Object addRoles(@RequestBody Map<String, Object> map) {
        String json = String.valueOf(map.get("role"));
        Role role = JSONObject.parseObject(json, Role.class);
        String json2 = String.valueOf(map.get("list"));
        System.out.println("json = " + json);
        System.out.println("json2 = " + json2);
        List<Integer> list = JSONObject.parseArray(json2, Integer.class);
        Integer[] arr = new Integer[list.size()];

        for (int i = 0; i < list.size(); i++) {
            arr[i] = list.get(i);
        }
        boolean b = roleService.addRole(role, arr);
        return b;
    }

    /**
     * 修改角色
     *
     * @param role
     * @param
     * @return
     */
    @RequiresPermissions(value = {"ROLE_EDIT"})
    @ResponseBody
    @RequestMapping(value = "/updateRoles", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public Object updateRoles(@RequestBody Role role, Integer[] user_roleKey) {
        System.out.println("role = " + role);
        List<User_RoleKey> list = new ArrayList<User_RoleKey>();
        for (int i = 0; i < user_roleKey.length; i++) {
            System.out.println("user_roleKey[i] = " + user_roleKey[i]);
            User_RoleKey userRoleKey = new User_RoleKey();
            userRoleKey.setUser_id(user_roleKey[i]);
            userRoleKey.setRole_id(role.getId());
            list.add(userRoleKey);
        }
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(role.getId());
        Boolean aBoolean = roleService.updateRole(role, example, list);
        return aBoolean;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"ROLE_DELETE"})
    @ResponseBody
    @RequestMapping(value = "/deleteRoles", produces = {"application/json;charset=utf-8"}, method = RequestMethod.GET)
    public Object deleteRoles(String id) {
        Boolean role = roleService.deleteRole(Integer.valueOf(id));
        return role;
    }

    /**
     * 显示用户
     *
     * @return
     */
    @RequiresPermissions(value = {"ROLE_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/showUser", produces = {"application/json;charset=utf-8"}, method = RequestMethod.GET)
    public Object showUser() {
        List<User> list = roleService.queryAllUser();
        return list;
    }

    /**
     * 显示菜单栏
     *
     * @return
     */
    @RequiresPermissions(value = {"MENU_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/showMenu", produces = {"application/json;charset=utf-8"}, method = RequestMethod.GET)
    public Object showMenu() {
        List<MenuParentTree> trees = menuService.queryAllParent();
        return trees;
    }

    /**
     * 根据查询出的集合来在复习框中选中
     *
     * @param roleID
     * @return
     */
    @RequiresPermissions(value = {"MENU_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/showMenuByRoleID", produces = {"application/json;charset=utf-8"}, method = RequestMethod.GET)
    public Object showMenuByRoleID(String roleID) {
        List<Integer> list = menuService.findMenuChildByMenuParent(Integer.valueOf(roleID));
        return list;
    }

    /**
     * 菜单编辑
     *
     * @param map
     * @return
     */
    @RequiresPermissions(value = {"MENU_EDIT"})
    @ResponseBody
    @RequestMapping(value = "/updateMenu", produces = {"application/json;charset=utf-8"}, method = RequestMethod.POST)
    public Object updateMenu(@RequestBody Map<String, Object> map) {

        /**
         * jackjson核心对象
         */
        String roleID = String.valueOf(map.get("roleID"));
        String menuIds = String.valueOf(map.get("menuIds"));
        ObjectMapper mapper = new ObjectMapper();
        Integer[] integers = null;
        try {
            integers = mapper.readValue(menuIds, Integer[].class);
            menuService.updateMenu(Integer.valueOf(roleID), integers);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 显示权限数据
     * @return
     */
    @RequiresPermissions(value = {"PERMISSION_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/showPermission", method = RequestMethod.GET, produces = {"application/json;charset=utf-8"})
    public Object showPermission() {
        List<Permission> list = userRolePermissionService.queryAllPermission();
        return list;
    }

    /**
     * 用于选中
     * @param id
     * @return
     */
    @RequiresPermissions(value = {"PERMISSION_QUERY"})
    @ResponseBody
    @RequestMapping(value = "/showPermissionByID",method = RequestMethod.GET,produces = {"application/json;charset=utf-8"})
    public Object showPermissionByID(String id){
        return userRolePermissionService.findPermissionID(Integer.valueOf(id));
    }

    /**
     * 编辑角色和权限之间的关系
     * @param map
     * @return
     */
    @RequiresPermissions(value = "PERMISSION_EDIT")
    @ResponseBody
    @RequestMapping(value = "/updatePermission",method = RequestMethod.POST,produces = {"application/json;charset=utf-8"})
    public Object updatePermission(@RequestBody Map<String,Object> map){
        String json=String.valueOf(map.get("roleID"));
                String json2=String.valueOf(map.get("permissionIds"));
                ObjectMapper mapper=new ObjectMapper();
        try {
            Integer roleID = mapper.readValue(json, Integer.class);
            Integer[] permissionIds = mapper.readValue(json2, Integer[].class);
            userRolePermissionService.updatePermission(roleID,permissionIds);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }
}
